package com.webside.points.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.webside.points.model.TopSearchEntity;
import com.webside.points.service.TopSearchService;
import com.webside.points.wechat.PageUtil;

@Controller
@Scope("prototype")
@RequestMapping("/topSearch/")
public class TopSearchController {

	
	@Autowired
	TopSearchService topSearchService;
	
	
	@RequestMapping("findAllUI")
	public String findAllUI(Model model){
		
		System.out.println("进入热搜查询：>>>");
		PageUtil pageUtil=new PageUtil();
		TopSearchEntity topSearchEntity = new TopSearchEntity();
		int totalCount=topSearchService.findCount(topSearchEntity);
		pageUtil.setTotalCount(totalCount);
		pageUtil.setCurPage(1);
		model.addAttribute("pageUtil", pageUtil);
		
		return "serve/topSearch";
	}
	
	@RequestMapping("findAll")
	public void findAll(Model model,TopSearchEntity topSearchEntity,HttpServletResponse response){
		System.out.println("进入热搜类型：>>>"+topSearchEntity);
		 PageUtil pageUtil=new PageUtil();
		 
		 if(String.valueOf(topSearchEntity.getCurPage())!=null){
			 int cPage=Integer.valueOf(topSearchEntity.getCurPage());
			 cPage=(cPage-1)*10;
			 topSearchEntity.setCurPage(Integer.valueOf(cPage));
		 }
		 
		 List<TopSearchEntity> list=topSearchService.findAll(topSearchEntity);
		 System.out.println(list);
		  pageUtil.setContent(list);
		  model.addAttribute("pageUtil", pageUtil);
		  
		  String json = JSON.toJSONString(pageUtil);
			try {
				response.getWriter().write(json);
				response.flushBuffer();
			} catch (Exception e) {
				System.out.println("JsonError："+e);
			}
	}
	
	@RequestMapping("insert")
	public String insert(TopSearchEntity topSearchEntity){
		
		topSearchEntity.setStatus("1");
		topSearchEntity.setCreateTime(new Date(System.currentTimeMillis()));
		System.out.println("进入文章类型增加：>>>"+topSearchEntity);
		
		System.out.println(topSearchService.insert(topSearchEntity));
		return "redirect:findAllUI";
	}
	
	@RequestMapping("delete")
	public String delete(int id){
		
		System.out.println(topSearchService.delete(id));
		return "redirect:findAllUI";
	}
	
	@RequestMapping("update")
	public String update(TopSearchEntity topSearchEntity){
		
		topSearchEntity.setCreateTime(new Date(System.currentTimeMillis()));
		
		System.out.println(topSearchService.update(topSearchEntity));
		return "redirect:findAllUI";
	}
	
	@RequestMapping("findById")
	public String findById(int id,Model model){
		
		model.addAttribute("topSearch",topSearchService.findById(id));
		return "serve/topSearchForm";
	}
}
